package com.tbsoft.ehr.service.attendance;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.tbsoft.ehr.dao.attendance.AttendanceDao;
import com.tbsoft.ehr.dao.staffFile.StaffFileDao;
import com.tbsoft.ehr.model.Attendance;
import com.tbsoft.ehr.model.StaffInfo;
import com.tbsoft.ehr.model.YearLeave;

public class AttendanceServiceImpl implements AttendanceService {

	private AttendanceDao attendanceDao;
	private StaffFileDao staffFileDao;
	
	public AttendanceDao getAttendanceDao() {
		return attendanceDao;
	}

	public void setAttendanceDao(AttendanceDao attendanceDao) {
		this.attendanceDao = attendanceDao;
	}

	public StaffFileDao getStaffFileDao() {
		return staffFileDao;
	}

	public void setStaffFileDao(StaffFileDao staffFileDao) {
		this.staffFileDao = staffFileDao;
	}

	@Override
	public boolean addAttendances(List<Attendance> attendances) {
		return attendanceDao.saveList(attendances);
	}

	@Override
	public List<YearLeave> queryYearLeaveByCondton(StaffInfo staffInfo,int startIndex,int count) {
		List<StaffInfo> staffLst = staffFileDao.getStaffInfoByCondton(staffInfo, null, null, startIndex, count);			
		return calculatYearLeave(staffLst);
	}	
	
	@Override
	public List<YearLeave> queryYearLeaveByUserId(String userId){
		List<StaffInfo> staffLst = staffFileDao.getStaffFileByStaffName(userId);
		return calculatYearLeave(staffLst);
	}
	
	/**
	 * 计算年假
	 * @param staffLst
	 * @return
	 */
	private List<YearLeave> calculatYearLeave(List<StaffInfo> staffLst){
		List<YearLeave> yearLeavLst = new ArrayList<YearLeave>();		
		for(StaffInfo si:staffLst) {			
			//入职日期
			Calendar entryDate=Calendar.getInstance();
			entryDate.setTime(si.getEntrydate());
			//设置为转正时间
			entryDate.set(Calendar.MONTH, entryDate.get(Calendar.MONTH)+2);	
			//设置为转正后且满一年(此时开始有年假可休)
			entryDate.set(Calendar.YEAR, entryDate.get(Calendar.YEAR)+1);
			
			List<Attendance> attds = new ArrayList<Attendance>();
			//当前时间
			Calendar currentDate = Calendar.getInstance();
			int yearLeaveDays=0;
			//判断是否可休年假
			if(currentDate.after(entryDate)) {
				//计算可休年假天数
				yearLeaveDays=5;
				entryDate.set(Calendar.YEAR, entryDate.get(Calendar.YEAR)+1);
				while(currentDate.after(entryDate)){
					yearLeaveDays++;
					entryDate.set(Calendar.YEAR, entryDate.get(Calendar.YEAR)+1);
				}
				//查询最近可休区间已休年假数
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				String endDate = sdf.format(entryDate.getTime());
				entryDate.set(Calendar.YEAR, entryDate.get(Calendar.YEAR)-1);		
				String startDate = sdf.format(entryDate.getTime());
				attds = attendanceDao.getYearLeaveByUserId(si.getUserId(),startDate,endDate);
			} 
			//上限为15天
			if(yearLeaveDays>15){
				yearLeaveDays=15;
			}
			YearLeave yearLeave = new YearLeave();
			yearLeave.setUserId(si.getUserId());
			yearLeave.setName(si.getName());
			yearLeave.setEntrydate(si.getEntrydate());
			//年假总天数
			yearLeave.setTotalDay(yearLeaveDays);
			//已使用天数
			yearLeave.setUsedDay(attds.size());
			//未使用天数
			if(yearLeaveDays-attds.size()>0){				
				yearLeave.setUnusedDay(yearLeaveDays-attds.size());
			}
			yearLeavLst.add(yearLeave);
		}
		return yearLeavLst;
	}

	@Override
	public List<Attendance> getAbsenceByCondton(String name,
			String adTimeFrom, String adTimeTo, int startIndex, int count) {
		List<StaffInfo> staffLst = null;
		if(name!=null&&!name.isEmpty()){
			staffLst = staffFileDao.getStaffFileByStaffName(name);	
			if(staffLst.size()==0) {
				return null;
			}
		}
		return attendanceDao.getAbsenceByCondton(staffLst, adTimeFrom, adTimeTo, startIndex, count);
	}

	@Override
	public int getAbsenceCount(String name, String adTimeFrom, String adTimeTo) {
		List<StaffInfo> staffLst = null;
		if(name!=null&&!name.isEmpty()){
			staffLst = staffFileDao.getStaffFileByStaffName(name);	
			if(staffLst.size()==0) {
				return 0;
			}
		}
		return attendanceDao.getAbsenceCount(staffLst, adTimeFrom, adTimeTo);
	}

	@Override
	public Attendance getAttendanceById(String id) {
		return attendanceDao.getAttendanceById(id);
	}

	@Override
	public void deleteAbsence(Attendance ad) {
		attendanceDao.delete(ad);
	}

	@Override
	public void updateAbsence(Attendance ad) {
		attendanceDao.updateAbsence(ad);
	}

	@Override
	public boolean hasAbsenceInDate(String userId, Date d) {
		return attendanceDao.hasAbsenceInDate(userId, d);
	}

	@Override
	public BigDecimal getDaysOffByStaffId(String staffId) {
		BigDecimal sum = new BigDecimal("0");
		List<Attendance> adLst = attendanceDao.getDaysOffByStaffId(staffId);
		for(Attendance ad:adLst) {
			sum = sum.add(ad.getHours());
		}
		return sum;
	}

}
